package com.cdm.beans.cdm;

import java.sql.Timestamp;
import java.util.List;

import net.sf.frails.JSFHelper;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;

import com.cdm.domain.dao.ContentDAO;
import com.cdm.domain.dao.UserDAO;
import com.cdm.domain.service.Service;
import com.cdm.domain.service.ServiceImpl;
import com.cdm.persistence.Content;
import com.cdm.persistence.ContentState;
import com.cdm.persistence.ContentUse;
import com.cdm.persistence.EntityManagerHelper;
import com.cdm.persistence.User;
import com.cdm.util.Constants;
import com.vivo.util.DTool;

@DefMbean
public class ContentInfoBean {

	@Prop
	User loginUser;

	@Prop
	List<Content> contents;

	// @Prop
	// List<ContentUse> contentsUse;

	@InVar
	Content content;

	@PreRender(failOutcome = "/content/contentInfo")
	public boolean init() {
		Object object = JSFHelper.getFromApplication(Constants.LOGIN_USER_KEY);
		if (object instanceof User && object != null) {
			loginUser = (User) object;
		}

		contents = contentDAO.findAll();
		System.out.println(contents.get(0) + "  |  " + contents.get(0).getContentState() + "  |  "
		        + contents.get(0).getContentState().getContentState());
		return true;
	}

	@InjectBean
	public String dobutton() {
		if (content == null) {
			return "";
		}
		int state = content.getContentState().getContentState();
		switch (state) {
		case 0:
			service.changeContentState(content, ContentState.DECRYPTED);
			return "/content/contentDecrypt";
		case 1:
			service.changeContentState(content, ContentState.TRANSCODED);
			return "/content/contentTranscode";
		case 2:
			service.changeContentState(content, ContentState.ENCRYPTED);
			return "/content/contentEecrypt2";
		default:
			break;
		}

		return "";
	}

	private ServiceImpl service = new ServiceImpl();
	private ContentDAO contentDAO = new ContentDAO();

}
